﻿#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MCMLXVII.BackEnd.Services.Common.Messages;
using MCMLXVII.BackEnd.Services.OrganizationChart.Messages;
using MCMLXVII.Core.Common;

#endregion

namespace MCMLXVII.Tools.OrganizationChart
{
    partial class frmMembersSelector : Form
    {
        private int _MemberClass;
        msgMembershipInfo _ActualMember;
        msgMembershipInfo[] _FindResults;

        public msgMembershipInfo ActualMember
        {
            get
            {
                return _ActualMember;
            }
        }



        public frmMembersSelector(msgMembershipInfo ActualMember, int MemberClass, string MemberName)
        {
            InitializeComponent();
            txtActualMember.Text = ActualMember.MemberID.ToString() + " - " + ActualMember.Description;
            _MemberClass = MemberClass;
            _ActualMember = ActualMember;
        }

        public frmMembersSelector()
        {
            InitializeComponent();
            txtActualMember.Text = "";
            _MemberClass = 0;
            _ActualMember = new msgMembershipInfo(0, 1, "..."); ;
        }

 
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            this.Enabled = false;
            try
            {
                _FindResults = MembersClasses.MemberBase.FindRoleMembershipInfo(_MemberClass, txtFindPattern.Text);
                lvwSearchResults.View = View.Details;
                lvwSearchResults.Items.Clear();
                if (_FindResults != null)
                {
                    foreach (msgMembershipInfo member in _FindResults)
                    {
                        ListViewItem it = lvwSearchResults.Items.Add(member.MemberID.ToString());
                        it.SubItems.Add(member.Description);
                    }
                }
            }
            catch
            {
                MessageBox.Show(Properties.Resources.UnableToFind);
            }
            finally 
            {
                this.Cursor = Cursors.Default;
                this.Enabled = true;
            }
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void lvwSearchResults_MouseDoubleClick(object sender, MouseEventArgs e)
        {
        }

        private void lvwSearchResults_DoubleClick(object sender, EventArgs e)
        {
            if (lvwSearchResults.SelectedItems.Count == 1)
            {
                int itemid = Int32.Parse(lvwSearchResults.SelectedItems[0].Text);
                foreach (msgMembershipInfo member in _FindResults)
                {
                    if (member.MemberID == itemid)
                    {
                        txtActualMember.Text = member.MemberID.ToString() + " - " + member.Description;
                        _ActualMember = member;
                        btnSave.Enabled = true;
                        break;
                    }
                }
            }
        }

        private void lvwSearchResults_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}